import 'package:flustars/flustars.dart';
import 'package:osc/utils/string_util.dart';

class OscSp {
  // 工厂模式
  factory OscSp() =>getInstance();
  static OscSp get instance => getInstance();
  static OscSp _instance;
  String code;
  String accessToken;
  String refreshToken;
  String tokenType;
  int uid;
  int expiresIn;
  int saveTime;

  String lastSearchName;

  OscSp._internal() {
    // 初始化
    init();
  }
  static OscSp getInstance() {
    if (_instance == null) {
      _instance = new OscSp._internal();
    }
    return _instance;
  }

  init(){
    getCode();
    getUId();
    getAccessToken();
    getRefreshToken();
    getTokenType();
    getExpiresIn();
    getSaveTime();
  }



  setUId(int uid){
    if(uid > 0){
      this.uid = uid;
      setInt("osc_uid", uid);
    }
  }
  int getUId(){
    if(uid == null || uid == 0){
      uid = getInt("osc_uid");
    }
    return uid;
  }
  setExpiresIn(int expiresIn){
    if(expiresIn > 0){
      this.expiresIn = expiresIn;
      setInt("osc_expires_in", expiresIn);
      setSaveTime(DateTime.now().millisecondsSinceEpoch);
    }
  }
  int getExpiresIn(){
    if(expiresIn == null || expiresIn == 0){
      expiresIn = getInt("osc_expires_in");
    }
    return expiresIn;
  }
  setSaveTime(int saveTime){
    if(saveTime > 0){
      this.saveTime = saveTime;
      setInt("osc_saveTime", saveTime);
    }
  }
  int getSaveTime(){
    if(saveTime == null || saveTime == 0){
      saveTime = getInt("osc_saveTime");
    }
    return saveTime;
  }
  setCode(String code){
    if(!StringUtil.isEmpty(code)){
      this.code = code;
      setString("osc_code", code);
    }
  }

  String getCode(){
    if(StringUtil.isEmpty(code)){
      this.code = getString("osc_code");
    }
    return code;
  }
  setAccessToken(String accessToken){
    if(!StringUtil.isEmpty(accessToken)){
      this.accessToken = accessToken;
      setString("osc_accessToken", accessToken);
    }
  }
  String getAccessToken(){
    if(StringUtil.isEmpty(accessToken)){
      this.accessToken = getString("osc_accessToken");
    }
    return accessToken;
  }
  setRefreshToken(String refreshToken){
    if(!StringUtil.isEmpty(refreshToken)){
      this.refreshToken = refreshToken;
      setString("osc_refreshToken", refreshToken);
    }
  }
  String getRefreshToken(){
    if(StringUtil.isEmpty(refreshToken)){
      this.refreshToken = getString("osc_refreshToken");
    }
    return refreshToken;
  }

  setTokenType(String tokenType){
    if(!StringUtil.isEmpty(tokenType)){
      this.tokenType = tokenType;
      setString("osc_tokenType", tokenType);
    }
  }
  String getTokenType(){
    if(StringUtil.isEmpty(tokenType)){
      this.tokenType = getString("osc_tokenType");
    }
    return tokenType;
  }

  setLastSearchName(String name){
    if(!StringUtil.isEmpty(name)){
      this.lastSearchName = name;
      setString("lastSearchName", name);
    }
  }

  getLastSearchName(){
    if(StringUtil.isEmpty(lastSearchName)){
      lastSearchName = getString("lastSearchName");
    }
    return lastSearchName;
  }

  setString(String key, String value){
    if(StringUtil.isEmpty(key) || StringUtil.isEmpty(value)){
      return;
    }
    SpUtil.putString(key, value);
  }
  setInt(String key, int value){
    if(StringUtil.isEmpty(key) || value < 1){
      return;
    }
    SpUtil.putInt(key, value);
  }
  setBool(String key, bool value){
    if(StringUtil.isEmpty(key)){
      return;
    }
    SpUtil.putBool(key, value);
  }
  String getString(String key){
    String value = SpUtil.getString(key);
    return value;
  }
  int getInt(String key){
    return SpUtil.getInt(key);
  }
  bool getBool(String key){
    return SpUtil.getBool(key);
  }
}